package com.glsc.ngateway.common.base.converter.platform;

import com.alibaba.excel.util.CollectionUtils;
import com.glsc.ngateway.common.api.oaflow.dto.SystemFlowRelationResultDto;
import com.glsc.ngateway.common.api.oaflow.dto.VITSystemOAFlowResourceDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.FlowFormSystemNameApply;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.FlowFormSystemNameModify;
import com.glsc.ngateway.common.base.domain.oracle.ecology.VITSystemOAFlowResource;
import com.glsc.ngateway.common.base.domainmapper.SystemFlowRelationMapper;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhengyr
 * @date 2023/8/15-18:22
 */
public class SystemFlowConverter {
    private static final String APPLYFLOWTYPE = "系统名称申请";
    private static final String MODIFYFLOWTYPE = "系统名称变更";

    public static SystemFlowRelationResultDto convertFromSNA(FlowFormSystemNameApply domain) {
        SystemFlowRelationResultDto flowDto = SystemFlowRelationMapper.INSTANCE.domain2dtoApply(domain);
        flowDto.setProcessDefineKeyName(APPLYFLOWTYPE);
        return flowDto;
    }

    public static List<SystemFlowRelationResultDto> convertFromSNA(List<FlowFormSystemNameApply> doList) {
        return CollectionUtils.isEmpty(doList) ? java.util.Collections.emptyList() : doList.stream().map(SystemFlowConverter::convertFromSNA).collect(Collectors.toList());
    }

    public static SystemFlowRelationResultDto convertFromSNM(FlowFormSystemNameModify domain) {
        SystemFlowRelationResultDto flowDto = SystemFlowRelationMapper.INSTANCE.domain2dtoModify(domain);
        flowDto.setProcessDefineKeyName(MODIFYFLOWTYPE);
        return flowDto;
    }

    public static List<SystemFlowRelationResultDto> convertFromSNM(List<FlowFormSystemNameModify> doList) {
        return CollectionUtils.isEmpty(doList) ? java.util.Collections.emptyList() : doList.stream().map(SystemFlowConverter::convertFromSNM).collect(Collectors.toList());
    }

    /**
     * oa流程do转换dto
     */
    public static SystemFlowRelationResultDto convertFromOAFlow(VITSystemOAFlowResource domain) {
        SystemFlowRelationResultDto flowDto = SystemFlowRelationMapper.INSTANCE.domain2dtoOAFlow(domain);
        flowDto.setProcessTitle(domain.getRequestName());
        flowDto.setProcessId(domain.getRequestId());
        flowDto.setProcessDefineKeyName(domain.getWorkflowName());
        flowDto.setFlowCreateOperatorName(domain.getUserName());
        flowDto.setProcessCreateTime(LocalDate.parse(domain.getApplyDate()).atStartOfDay());
        return flowDto;
    }

    public static SystemFlowRelationResultDto convertFromOAFlow(VITSystemOAFlowResourceDto domain) {
        SystemFlowRelationResultDto flowDto = SystemFlowRelationMapper.INSTANCE.domain2dtoOAFlow(domain);
        flowDto.setProcessTitle(domain.getRequestName());
        flowDto.setProcessId(domain.getRequestId());
        flowDto.setProcessDefineKeyName(domain.getWorkflowName());
        flowDto.setFlowCreateOperatorName(domain.getUserName());
        flowDto.setProcessCreateTime(LocalDate.parse(domain.getApplyDate()).atStartOfDay());
        return flowDto;
    }

    public static List<SystemFlowRelationResultDto> convertFromOAFlow(List doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return java.util.Collections.emptyList();
        }
        return (List<SystemFlowRelationResultDto>) doList.stream()
                .map(d -> {
                    if (d instanceof VITSystemOAFlowResourceDto) {
                        return SystemFlowConverter.convertFromOAFlow((VITSystemOAFlowResourceDto) d);
                    } else if (d instanceof VITSystemOAFlowResource) {
                        return SystemFlowConverter.convertFromOAFlow((VITSystemOAFlowResource) d);
                    }
                    return null;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

}
